﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CoreUC
{
    /// <summary>
    /// Change password form control
    /// </summary>
    /// <remarks>trungpv@vnext.vn</remarks>
    public partial class ChangePasswordForm : DevComponents.DotNetBar.Office2007Form
    {
        #region Attributs and Getter/Setter
        private string m_currentPassword;
        private string m_newPassword;

        public string NewPassword
        {
            get { return m_newPassword; }
            set { m_newPassword = value; }
        }

        public string CurrentPassword
        {
            get { return m_currentPassword; }
            set { m_currentPassword = value; }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="password"></param>
        public ChangePasswordForm(string password)
        {
            InitializeComponent();
            m_newPassword = m_currentPassword = password; // Init password
        }

        #region Events on form
        private void ChangePasswordForm_Load(object sender, EventArgs e)
        {
            tbPassword.Select(); // Focus to password TextBox
        }

        /// <summary>
        /// Save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            changePassword();
        }

        /// <summary>
        /// Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Pressed enter key
        /// </summary>
        /// <param name="e"></param>
        private void keyPressEvent(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                changePassword();
            }
        }

        /// <summary>
        /// Pressed password TextBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            keyPressEvent(e);
        }

        /// <summary>
        /// Pressed new password TextBox 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbNewPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            keyPressEvent(e);
        }

        /// <summary>
        /// Pressed confirmation password
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbPasswordConfirm_KeyPress(object sender, KeyPressEventArgs e)
        {
            keyPressEvent(e);
        }
        #endregion

        #region Logic functions
        /// <summary>
        /// Start checking and changing password
        /// </summary>
        private void changePassword()
        {
            if (!checkPasswordEntered()) return;
            m_newPassword = tbNewPassword.Text; // change password
            System.Windows.Forms.MessageBox.Show(Messages.SAVE_SUCCES, Messages.NOTE, MessageBoxButtons.OK,
                MessageBoxIcon.Information);
            this.Close();
        }

        /// <summary>
        /// Check validation of text entered to TextBox
        /// </summary>
        /// <returns></returns>
        private bool checkValidateTextBox()
        {
            // Check validate
            if (String.IsNullOrEmpty(tbPassword.Text))
            {
                System.Windows.Forms.MessageBox.Show(Messages.PASSWORD_EMPTY_NULL, Messages.WARINING,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbPassword.Select();
                return false;
            }
            if (String.IsNullOrEmpty(tbNewPassword.Text))
            {
                System.Windows.Forms.MessageBox.Show(Messages.NEW_PASSWORD_EMPTY_NULL, Messages.WARINING,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbNewPassword.Select();
                return false;
            }
            if (String.IsNullOrEmpty(tbPasswordConfirm.Text))
            {
                System.Windows.Forms.MessageBox.Show(Messages.CONFIRM_PASSWORD_EMPTY_NULL, Messages.WARINING,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbPasswordConfirm.Select();
                return false;
            }
            return true;
        }

        /// <summary>
        /// Check password
        /// </summary>
        /// <returns></returns>
        private bool checkPasswordEntered()
        {
            // Wrong password
            if (m_currentPassword != tbPassword.Text)
            {
                System.Windows.Forms.MessageBox.Show(Messages.WRONG_PASSWORD, Messages.WARINING,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                tbPassword.Text = "";
                tbPassword.Select();
                return false;
            }
            // New password not match
            if (tbNewPassword.Text != tbPasswordConfirm.Text)
            {
                System.Windows.Forms.MessageBox.Show(Messages.NEW_PASSWORD_NOT_MATCH, Messages.WARINING,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbNewPassword.Text = "";
                tbPasswordConfirm.Text = "";
                tbNewPassword.Select();
                return false;
            }
            return true;
        }
        #endregion
    }
}
